package com.ymj.demoback.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ymj.demoback.constant.UserConstant;
import com.ymj.demoback.model.domain.User;
import com.ymj.demoback.model.domain.request.UserLoginRequest;
import com.ymj.demoback.model.domain.request.UserRegisterRequest;
import com.ymj.demoback.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.ymj.demoback.constant.UserConstant.ADMIN_ROLE;

/**
 * 用户接口
 *
 * @author ymj
 */
@RestController// 适用于编写 restful 风格的 api,返回值默认为 json 类型
@RequestMapping("/user")// 请求路径
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/register")
    public Long userRegister(@RequestBody UserRegisterRequest userRegisterRequest){
        if (userRegisterRequest == null){
            return null;
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount,userPassword,checkPassword)){
            return null;
        }
        return userService.UserRegister(userAccount, userPassword, checkPassword);
    }
    @PostMapping("/login")
    public User userLogin(@RequestBody UserLoginRequest UserLoginRequest, HttpServletRequest request){
        if (UserLoginRequest == null){
            return null;
        }
        String userAccount = UserLoginRequest.getUserAccount();
        String userPassword = UserLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount,userPassword)){
            return null;
        }
        return userService.userLogin(userAccount,userPassword,request);
    }

    @GetMapping("/search")
//    查询接口
    public List<User> searchUsers(String username,HttpServletRequest request){
        if (!isAsdmin(request)){
            return new ArrayList<>();
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(username)){
            queryWrapper.like("username",username);
        }
        List<User> userList = userService.list(queryWrapper);
        return userList.stream().map(user -> {
            user.setUserPassword(null);
            return userService.getSafetyUser(user);
        }).collect(Collectors.toList());
    }

    @PostMapping("/delete")
//    删除接口，接收前端的参数 id;通过 mybatis-plus 框架，逻辑删除
    public boolean deleteUser(@RequestBody long id,HttpServletRequest request){
        if (!isAsdmin(request)){
            return false;
        }
        if (id <= 0){
            return false;
        }
        return userService.removeById(id);
    }

    /**
     * 是否为管理员
     * @param request
     * @return
     */
    private boolean isAsdmin(HttpServletRequest request){

        //        仅管理员可查询
        Object userObj = request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        User user = (User) userObj;
        return user != null && user.getUserRole() == ADMIN_ROLE;
    }
}
